<?php

class ExpositionFacade 
{
    public static function getModel($id)
    {
        return Exposition::model()->findByPk($id);
    }
    
    public static function getActualExpositions()
    {
        $now = date('Y-m-d H:i:s', time());        
        return Exposition::model()->findAll("to_date>=:now", array(':now' => $now));
    }
    
    public static function getCountActualExpositions()
    {
        $now = date('Y-m-d H:i:s', time());  
        return Exposition::model()->count("to_date>=:now", array(':now' => $now));
    }
    
    public static function getExpositionUploadsDir(Exposition $model)
    {
        Yii::app()->controller->checkDirExists(Yii::getPathOfAlias('webroot') . "/uploads/exposition");
        Yii::app()->controller->checkDirExists(Yii::getPathOfAlias('webroot') . "/uploads/exposition/" . $model->exposition_id);
        
        return Yii::getPathOfAlias('webroot') . "/uploads/exposition/" . $model->exposition_id . "/";
    }
    
    public static function saveImage($uploadImage, Exposition $model)
    {                
        $oldImageId = $model->picture_src;
        $model->picture_src = $uploadImage->image_id;   
        $model->from_date = strtotime($model->from_date);
        $model->to_date = strtotime($model->to_date);
        self::saveModel($model);
        if ($oldImageId != null)
        {
            UploadImageFacade::unsetImage($oldImageId);
        }  
    }        
    
    public static function saveModel(Exposition $model)
    {
        $model->save();
    }
    
    public static function getExpositionMembers($expositionId)
    {
        return ExpositionMember::model()->findAll("exposition_id=:expositionId", array(
            ":expositionId" => $expositionId
        ));
    }
    
    public static function getExpositionMembersCount($expositionId)
    {
        return ExpositionMember::model()->count("exposition_id=:expositionId", array(
            ":expositionId" => $expositionId
        ));
    }
    
    public static function isAlreadyGo($userId, $expositionId)
    {
        $now = date('Y-m-d H:i:s', time());  
        $actual = Exposition::model()->find("to_date>=:now AND exposition_id=:expositionId", array(
            ':now'          => $now,
            ':expositionId' => $expositionId
        ));
        
        if ($actual === null)
        {
            return true;
        }
        
        $result = ExpositionMember::model()->find("exposition_id=:expositionId AND user_id=:userId", array(
            ":expositionId" => $expositionId,
            ":userId"       => $userId
        ));
        
        if ($result === null)
        {            
            return false;
        }
        return true;
    }
    
    public static function addExpositionMember($userId, $expositionId)
    {
        $member = new ExpositionMember();
        $member->user_id = $userId;
        $member->exposition_id = $expositionId;
        $member->save();
    }
}

?>